package com.ccx.web.backand;

import com.ccx.model.EbProductCategory;
import com.ccx.service.EbProductCategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productCategory")
public class EbProductCategoryController {
    @Autowired
    EbProductCategoryService ebProductCategoryService;

    //查询所有大类小类
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "2") Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Map<String, List<EbProductCategory>> bigAndSmallCategory = ebProductCategoryService.findBigAndSmallCategory();
        List<EbProductCategory> bigs = bigAndSmallCategory.get("bigs");
        //System.out.println(((Page)bigs).getPages());
        model.addAttribute("categoryMap",bigAndSmallCategory);
        model.addAttribute("page",page);
        model.addAttribute("maxPage",((Page)bigs).getPages());
        return "manage/productClass";
    }

    //根据Id查询对应的大类或小类信息
    @RequestMapping("/{epcId}")
    public String edit(Model model, @PathVariable("epcId") Integer epcId){
        //查询大类信息
        List<EbProductCategory> big = ebProductCategoryService.findBig();
        //根据Id查询大类或小类信息
        EbProductCategory ebPC = ebProductCategoryService.findByBigAndSmallCategory(epcId);
        model.addAttribute("bigs",big);
        model.addAttribute("ebPC",ebPC);
        return "manage/productClass-modify";
    }

    //根据Id修改小类信息
    @RequestMapping("/edit")
    public String update(Model model, EbProductCategory ebProductCategory){
        int update = ebProductCategoryService.update(ebProductCategory);
        if(update<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    //获取大类信息
    @RequestMapping("/to_add")
    public String select(Model model){
        //查询大类信息
        List<EbProductCategory> big = ebProductCategoryService.findBig();
        model.addAttribute("big",big);
        return "manage/productClass-add";
    }

    //添加小类信息
    @RequestMapping("/add")
    public String save( EbProductCategory ebProductCategory){
        int insert = ebProductCategoryService.insert(ebProductCategory);
        if(insert<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    //删除大类或小类
    @RequestMapping("/delete")
    public String delete(int epcId){
        int i = ebProductCategoryService.deletefindByBig(epcId);
        if(i<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

}
